package com.gzhryc.shared_device.oem.micro.iot.controllers;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.api.protocol.*;
import com.gzhryc.shared_device.bxscn.core.IBXSCNNetworkService;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNExecuteResult;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNLeaseCommand;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNOpenSlotCommand;
import com.gzhryc.shared_device.cdb.api.protocol.*;
import com.gzhryc.shared_device.oem.micro.iot.LocalNetworkServiceUtils;
import com.gzhryc.shared_device.oem.micro.iot.MicroConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BXSCNOperateController {

    static Logger log = Logger.getLogger(CDCZOperateController.class);

    @Route(value = {"/api/v1/bxscn/lease.do"}, method = "POST")
    public BXSCNHttpLeaseResponse lease(HttpServletRequest request, HttpServletResponse response) {
        BXSCNHttpLeaseResponse apiResp = new BXSCNHttpLeaseResponse(DefaultAPICodeConstants.EXECUTE_ERROR,"操作失败，请稍后再试");
        BXSCNHttpLeaseRequest apiReq = WebUtils.getJsonRequest(request, BXSCNHttpLeaseRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
            Long iotId = WebUtils.getAttribute(request, MicroConstants.ATTR_IOT_ID, Long.class);
            log.info("{{0}}IOT节点触发便携式电池租借，请求：{{1:json}}",iotId,apiReq);
            IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
            if (networkService != null) {
                BXSCNLeaseCommand leaseCommand = new BXSCNLeaseCommand();
                leaseCommand.setDeviceSn(apiReq.getBody().getDeviceSn());
                leaseCommand.setSlotNum(apiReq.getBody().getSlotNum());
                leaseCommand.setTradeNo(apiReq.getBody().getTradeNo());
                leaseCommand.setMessageId(apiReq.getBody().getMessageId());

                BXSCNExecuteResult executeResult = networkService.lease(leaseCommand);
                if (executeResult != null) {
                    apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setResultCode(executeResult.getCode());
                    apiResp.getBody().setErrorMsg(executeResult.getMsg());
                    apiResp.getBody().setMessageId(executeResult.getMessageId());
                }
            } else {
                apiResp.setMsg("未找到共享便携式电池网络服务");
            }
        }
        return apiResp;
    }

    @Route(value = {"/api/v1/bxscn/open_slot.do"}, method = "POST")
    public BXSCNHttpReturnResponse openSlot(HttpServletRequest request, HttpServletResponse response) {
        BXSCNHttpReturnResponse apiResp = new BXSCNHttpReturnResponse(DefaultAPICodeConstants.EXECUTE_ERROR,"操作失败，请稍后再试");
        BXSCNHttpReturnRequest apiReq = WebUtils.getJsonRequest(request, BXSCNHttpReturnRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
            Long iotId = WebUtils.getAttribute(request, MicroConstants.ATTR_IOT_ID, Long.class);
            log.info("{{0}}IOT节点触发便携式电池归还，请求：{{1:json}}",iotId,apiReq);
            IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
            if (networkService != null) {
                BXSCNOpenSlotCommand returnCommand = new BXSCNOpenSlotCommand();
                returnCommand.setDeviceSn(apiReq.getBody().getDeviceSn());
                returnCommand.setSlotNum(apiReq.getBody().getSlotNum());
                returnCommand.setMessageId(apiReq.getBody().getMessageId());

                BXSCNExecuteResult executeResult = networkService.openSlot(returnCommand);
                if (executeResult != null) {
                    apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setResultCode(executeResult.getCode());
                    apiResp.getBody().setErrorMsg(executeResult.getMsg());
                    apiResp.getBody().setMessageId(executeResult.getMessageId());
                }
            } else {
                apiResp.setMsg("未找到共享便携式电池网络服务");
            }
        }
        return apiResp;
    }

    @Route(value = {"/api/v1/bxscn/eject.do"}, method = "POST")
    public BXSCNHttpEjectResponse eject(HttpServletRequest request, HttpServletResponse response) {
        BXSCNHttpEjectResponse apiResp = new BXSCNHttpEjectResponse(DefaultAPICodeConstants.EXECUTE_ERROR,"操作失败，请稍后再试");
        BXSCNHttpEjectRequest apiReq = WebUtils.getJsonRequest(request, BXSCNHttpEjectRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
            Long iotId = WebUtils.getAttribute(request, MicroConstants.ATTR_IOT_ID, Long.class);
            if(apiReq.getBody().getSlotNum() != null){
                log.info("{{0}}IOT节点触发便携式电池强制弹出，请求：{{1:json}}",iotId,apiReq);
                IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
                if (networkService != null) {
                    BXSCNExecuteResult executeResult = networkService.forceEjectDevice(apiReq.getBody().getDeviceSn(),apiReq.getBody().getSlotNum());
                    if (executeResult != null) {
                        apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                        apiResp.getBody().setResultCode(executeResult.getCode());
                        apiResp.getBody().setErrorMsg(executeResult.getMsg());
                        apiResp.getBody().setMessageId(executeResult.getMessageId());
                    }
                } else {
                    apiResp.setMsg("未找到共享便携式电池网络服务");
                }
            }else{
                log.info("{{0}}IOT节点触发便携式电池全部弹出，请求：{{1:json}}",iotId,apiReq);
                IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
                if (networkService != null) {
                    BXSCNExecuteResult executeResult = networkService.forceEjectDevice(apiReq.getBody().getDeviceSn());
                    if (executeResult != null) {
                        apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                        apiResp.getBody().setResultCode(executeResult.getCode());
                        apiResp.getBody().setErrorMsg(executeResult.getMsg());
                        apiResp.getBody().setMessageId(executeResult.getMessageId());
                    }
                } else {
                    apiResp.setMsg("未找到共享便携式电池网络服务");
                }
            }
        }
        return apiResp;
    }

    @Route(value = {"/api/v1/bxscn/search.do"}, method = "POST")
    public BXSCNHttpSearchResponse search(HttpServletRequest request, HttpServletResponse response) {
        BXSCNHttpSearchResponse apiResp = new BXSCNHttpSearchResponse(DefaultAPICodeConstants.EXECUTE_ERROR, "操作失败，请稍后再试");
        BXSCNHttpSearchRequest apiReq = WebUtils.getJsonRequest(request, BXSCNHttpSearchRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
            Long iotId = WebUtils.getAttribute(request, MicroConstants.ATTR_IOT_ID, Long.class);
            log.info("{{0}}IOT节点触发便携式电池查询，请求：{{1:json}}", iotId, apiReq);
            IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
            if (networkService != null) {
                BXSCNExecuteResult executeResult = networkService.searchDevice(apiReq.getBody().getDeviceSn());
                if (executeResult != null) {
                    apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setResultCode(executeResult.getCode());
                    apiResp.getBody().setErrorMsg(executeResult.getMsg());
                    apiResp.getBody().setMessageId(executeResult.getMessageId());
                }
            } else {
                apiResp.setMsg("未找到共享便携式电池网络服务");
            }
        }
        return apiResp;
    }

    @Route(value = {"/api/v1/bxscn/send_cmd.do"}, method = "POST")
    public BXSCNHttpSendCmdResponse sendCmd(HttpServletRequest request, HttpServletResponse response) {
        BXSCNHttpSendCmdResponse apiResp = new BXSCNHttpSendCmdResponse(DefaultAPICodeConstants.EXECUTE_ERROR, "操作失败，请稍后再试");
        CDBHttpSendCmdRequest apiReq = WebUtils.getJsonRequest(request, CDBHttpSendCmdRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn()) && StringTools.isNotBlank(apiReq.getBody().getContent())) {
            Long iotId = WebUtils.getAttribute(request, MicroConstants.ATTR_IOT_ID, Long.class);
            log.info("{{0}}IOT节点触发便携式电池任意报文，请求：{{1:json}}", iotId, apiReq);
            IBXSCNNetworkService networkService = LocalNetworkServiceUtils.getBXSCNNetworkService(iotId);
            if (networkService != null) {
                BXSCNExecuteResult executeResult = networkService.sendCmd(apiReq.getBody().getDeviceSn(),apiReq.getBody().getContent());
                if (executeResult != null) {
                    apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setResultCode(executeResult.getCode());
                    apiResp.getBody().setErrorMsg(executeResult.getMsg());
                    apiResp.getBody().setMessageId(executeResult.getMessageId());
                }
            } else {
                apiResp.setMsg("未找到共享便携式电池网络服务");
            }
        }
        return apiResp;
    }
}
